const path = require('path')
const { dependencies } = require('../package.json')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const { HotModuleReplacementPlugin, DefinePlugin } = require('webpack')

// -p / --optimize-minimize 启用 uglifyjs 压缩

const configure = {
  mode: 'development', // production 自动开启压缩功能
  target: 'electron-main',
  entry: {
    main: path.resolve(__dirname, '../src/main/index.ts'),
  },
  output: {
    filename: '[name].js',
    libraryTarget: 'commonjs2',
    path: path.resolve(__dirname, '../dist'),
  },
  externals: [...Object.keys(dependencies || {})],
  resolve: {
    extensions: ['.js', '.json', '.node', '.ts']
  },
  devtool: 'source-map',
  module: {
    rules: [
      {
        test: /\.(tsx?|jsx?)$/,
        exclude: /node_moudles/,
        use: [
          {
            loader: 'babel-loader',
            options: {
              presets: [
                '@babel/preset-env',
                '@babel/preset-typescript'
              ]
            }
          }
        ],
      },
      {
        test: /\.node$/,
        use: 'node-loader'
      }
    ],
  },
  plugins: [
    new CleanWebpackPlugin({ cleanOnceBeforeBuildPatterns: [path.resolve(__dirname, '../dist')] }),
    new HotModuleReplacementPlugin()
  ],
}

if (process.env.NODE_ENV !== 'production') {
  configure.devtool = '#cheap-module-eval-source-map'
} else {
  configure.devtool = 'hidden-source-map'
  configure.plugins.push(
    new DefinePlugin({
      'process.env.NODE_ENV': `'production'`
    })
  )
}
module.exports = configure;